package com.crowntit.domain.tag.service;

import com.crowntit.domain.tag.adapter.repository.ITagRepository;
import com.crowntit.domain.tag.model.entity.CrowdTagsJobEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TagService implements ITagService{

    @Resource
    private ITagRepository repository;

    @Override
    public void execTagsBatchJob(String tagId, String batchId) {
        log.info("人群标签批次任务 tagId:{} batchId:{}", tagId, batchId);

        // 1. 查询数据
        CrowdTagsJobEntity jobEntity = repository.queryCrowdTagsJobByTagIdBatchId(tagId, batchId);

        // 2. 采集业务数据

        // 3. 根据规则计算人群

        // 4. 写入数据: 数据库、redis
        List<String> userIdList = new ArrayList<>();
        userIdList.add("xiaofuge");
        userIdList.add("liergou");
        userIdList.add("xiaofuge02");
        userIdList.add("crowntit");
        userIdList.add("yaque");

        int cnt = 0;
        for (String userId : userIdList) {
            if (repository.addCrowdTagsDetail(userId, tagId)) {
                cnt++;
            }
        }

        if (cnt > 0){
            repository.updateCrowdTagsStatistics(tagId, cnt);
        }
    }
}
